var app=getApp();
var c=require('../common.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    flag: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var user_id=wx.getStorageSync('userInfo').user_id;
    var sql = 'w(user_id=' + user_id+')f(phone)l(1)';
    c.ajax(
      app.dbs+'user',
      { sql: sql},
      res=>{
        var phonen=res.data.phone+'';
        var phones = phonen.substr(0, 3) + '****' + phonen.substr(7);
        this.setData({ phone: phones, phonesend: phonen, user_id: user_id})
      }
    )
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
   sendcode() {
    
     var phone = this.data.phonesend

    var phoneReg = /(^1[3|4|5|7|8]\d{9}$)|(^09\d{8}$)/;
    if (!phoneReg.test(phone)) {
      wx.showToast({
        title: '手机号码有误',
      })
      return false;
    }

    var timer = null;
    var that = this
    this.setData({
      flag: 60
    })
    timer = setInterval(() => {
      if (this.data.flag == 0) {
        clearInterval(timer)
        that.setData({
          flag: 0
        })
      }
      else {
        that.setData({
          flag: that.data.flag - 1
        })
      }

    }, 1000)
    c.ajax(
      app.globalData.rootPath + "/index.php/index/Wxapi/sendcodelosspass",
      {
        phone: phone
      },
      res => {
        wx.showToast({
          title: '发送成功',
        })
      }
    )
  },
  inputpass:function(e){
    var value=e.detail.value;
    this.setData({ value: value})
  },
  submitinfo:function(){
    var value = this.data.value;
    var phone = this.data.phonesend;
    var user_id=this.data.user_id;
    c.ajax(
      app.url +'VerPhone',
      { value: value, phone: phone},
      res=>{
           if(res.data==0){
             wx.showToast({
               title: '验证码错误',
               icon:'none'
             })
           } else if (res.data == 1){
                wx.navigateTo({
                  url: '/pages/sendneedsetpass2/sendneedsetpass2?user_id=' + user_id + '&value=' + value,
                })
           }
      }
    )
  }
})